<template>
    <el-dialog :visible.sync="open" :title="id ? '修改' : '添加'" width="30%" append-to-body :before-close="closeDelDialog">

        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

            <el-form-item label="敏感词名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
                <el-select v-model="form.state" style="width:100%" placeholder="请选择状态">
                    <el-option v-for="(item, index) in stateList" :key="index" :label="item.label"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="beizhu">
                <el-input type="textarea" v-model="form.beizhu"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="closeDelDialog">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { minganci, minganciInfo, minganciedit } from "@/api/archive/archivalAppraisal/openness/sensitive"
export default {
    props: ["open", "stateList", "id"],
    watch: {
        id: {
            handler(val) {
                console.log(val, "++++")
                if (val) {
                    //修改
                    minganciInfo(val).then(res => {
                        console.log(res, "++++")
                        this.form.name = res.data.name;
                        this.form.state = res.data.state;
                        this.form.beizhu = res.data.beizhu;
                        this.form.id = res.data.id;
                    })
                } else {
                    //添加
                }
            },
            immediate: true
        }
    },
    data() {
        return {
            form: {
                name: undefined,
                state: undefined,
                beizhu: undefined
            },
            rules: {
                name: [{ required: true, message: '请输入敏感词名称', trigger: 'blur' },],
                state: [{ required: true, message: '请选择状态', trigger: 'change' }]
            }
        }

    },
    created() {

    },
    methods: {
        closeDelDialog() {
            this.$refs.ruleForm.resetFields();
            this.$emit("closeDelDialog")
        },
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    if (this.id) {
                        minganciedit(this.form).then(res => {
                            if (res.code == "200") {
                                this.$modal.msgSuccess(res.msg);
                                this.$refs.ruleForm.resetFields();
                                this.$emit("submitForm")
                            }
                        })

                    } else {
                        minganci(this.form).then(res => {
                            if (res.code == "200") {
                                this.$modal.msgSuccess(res.msg);
                                this.$refs.ruleForm.resetFields();
                                this.$emit("submitForm")
                            }
                        })

                    }

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });

        },
    }
}
</script>